package edu.yngsxy.demo.controller;

import edu.yngsxy.demo.model.Result;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController
public class FileUploadController {

    @PostMapping("/upload")
    public Result<String> upload(MultipartFile imageurl) throws IOException {
        //把文件内容存储到本地磁盘

        String originalFilename = imageurl.getOriginalFilename();//获取文件名字

        /**
         * 但是文件名一致的话，之前的文件就会被覆盖
         * 所有我们要么用时间戳，要么用UUID
         * 我们这里用UUID
         */
        String filename = UUID.randomUUID().toString()+originalFilename.substring(originalFilename.lastIndexOf("."));
        imageurl.transferTo(new File("E:\\代码\\WebstormProjects\\vue\\secondhandBoot\\src\\images\\" + filename));
        return Result.success(filename);
    }
}
